package com.laiketui.admin.store.controller.store;

import com.laiketui.core.domain.Result;
import com.laiketui.core.lktconst.GloabConst;
import com.laiketui.domain.mch.AdminModel;
import com.laiketui.order.api.dto.StoreOrderDTO;
import com.laiketui.order.api.feign.StoreOrderFeign;
import com.laiketui.order.api.params.pay.BalancePayParams;
import com.laiketui.order.api.params.store.*;
import com.laiketui.root.annotation.HttpApiMethod;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLDecoder;

@Api(tags = "后台-采购订单")
@RestController
@RequestMapping("/v2/order/purchase")
public class PurchaseOrderController extends AdminBaseController{

    @Autowired
    private StoreOrderFeign storeOrderFeign;

    @Autowired
    private ExportService exportService;

    @ApiOperation("订单列表")
    @PostMapping("/list")
    @HttpApiMethod(apiKey = "admin.orderV2.purchaseList")
    public Result orderList(@Validated StoreOrderListParams params){
        AdminModel adminModel = currentAdmin(params);
        params.setStoreId(adminModel.getStore_id());
        params.setAdminId(adminModel.getId());
        params.setCurrent(params.getPageNum());
        return Result.success(storeOrderFeign.orderList(params));
    }

    @ApiOperation("订单详情")
    @PostMapping("/info")
    @HttpApiMethod(apiKey = "admin.orderV2.purchaseInfo")
    public Result info(@Validated StoreOrderInfoParams params){
        AdminModel adminModel = currentAdmin(params);
        params.setStoreId(adminModel.getStore_id());
        params.setAdminId(adminModel.getId());
        return Result.success(storeOrderFeign.getOrderInfo(params));
    }

    @ApiOperation("客服上传支付凭证")
    @PostMapping("/offlinePay")
    @HttpApiMethod(apiKey = "admin.orderV2.purchaseofflinePay")
    public Result offlinePay(@Validated BalancePayParams params){
        AdminModel adminModel = currentAdmin(params);
        params.setStoreId(adminModel.getStore_id());
        return Result.success(storeOrderFeign.offlinePay(params));
    }

    @ApiOperation("修改期望发货时间")
    @HttpApiMethod(apiKey = "admin.orderV2.editExpectedDeliveryTime")
    @PostMapping("/editExpectedDeliveryTime")
    public Result editExpectedDeliveryTime(@Validated StoreEditExpectedDeliveryTimeParams params){
        AdminModel adminModel = currentAdmin(params);
        params.setStoreId(adminModel.getStore_id());
        params.setAdminId(adminModel.getId());
        return Result.success(storeOrderFeign.editExpectedDeliveryTime(params));
    }

    //修改收货地址
    @ApiOperation("修改收货地址")
    @HttpApiMethod(apiKey = "admin.orderV2.editDelivery")
    @PostMapping("/editDelivery")
    public Result editDelivery(@Validated StoreEditDeliveryParams params){
        AdminModel adminModel = currentAdmin(params);
        params.setStoreId(adminModel.getStore_id());
        params.setAdminId(adminModel.getId());
        return Result.success(storeOrderFeign.editDelivery(params));
    }

    //修改工厂备注
    @ApiOperation("修改工厂备注")
    @HttpApiMethod(apiKey = "admin.orderV2.editMchRemark")
    @PostMapping("/editMchRemark")
    public Result editMchRemark(@Validated StoreEditMchRemarkParams params){
        AdminModel adminModel = currentAdmin(params);
        params.setStoreId(adminModel.getStore_id());
        params.setAdminId(adminModel.getId());
        return Result.success(storeOrderFeign.editMchRemark(params));
    }
    @ApiOperation("修改订单备注")
    @HttpApiMethod(apiKey = "admin.orderV2.editRemark")
    @PostMapping("/editRemark")
    public Result editRemark(@Validated StoreEditRemarkParams params){
        AdminModel adminModel = currentAdmin(params);
        params.setStoreId(adminModel.getStore_id());
        params.setAdminId(adminModel.getId());
        return Result.success(storeOrderFeign.editRemark(params));
    }

    @ApiOperation("修改物流单号")
    @HttpApiMethod(apiKey = "admin.orderV2.editDeliverNo")
    @PostMapping("/editDeliverNo")
    public Result editDeliverNo(@Validated StoreEditDeliverNoParams params){
        AdminModel adminModel = currentAdmin(params);
        params.setStoreId(adminModel.getStore_id());
        params.setAdminId(adminModel.getId());
        return Result.success(storeOrderFeign.editDeliverNo(params));
    }

    @ApiOperation("修改质检状态")
    @HttpApiMethod(apiKey = "admin.orderV2.editQualityInspection")
    @PostMapping("/editQualityInspection")
    public Result editQualityInspection(@Validated StoreEditQualityInspectionParams params){
        AdminModel adminModel = currentAdmin(params);
        params.setStoreId(adminModel.getStore_id());
        params.setAdminId(adminModel.getId());
        return Result.success(storeOrderFeign.editQualityInspection(params));
    }

    //完成收货
    @ApiOperation("完成收货")
    @HttpApiMethod(apiKey = "admin.orderV2.finishOrderParent")
    @PostMapping("/finishOrderParent")
    public Result finishOrderParent(@Validated StoreFinishParams params){
        AdminModel adminModel = currentAdmin(params);
        params.setStoreId(adminModel.getStore_id());
        params.setAdminId(adminModel.getId());
        return Result.success(storeOrderFeign.finishOrderParent(params));
    }

    //跟踪订单
    @ApiOperation("跟踪订单")
    @HttpApiMethod(apiKey = "admin.orderV2.addOrderParentMessage")
    @PostMapping("/addOrderParentMessage")
    public Result addOrderParentMessage(@Validated StoreAddParentMessageParams params){
        AdminModel adminModel = currentAdmin(params);
        params.setStoreId(adminModel.getStore_id());
        params.setAdminId(adminModel.getId());
        return Result.success(storeOrderFeign.addOrderParentMessage(params));
    }

    @ApiOperation("修改预计发货时间")
    @HttpApiMethod(apiKey = "admin.orderV2.editEstimatedDeliveryTime")
    @PostMapping("/editEstimatedDeliveryTime")
    public Result editEstimatedDeliveryTime(@Validated StoreMchOrderConfirmParams params){
        AdminModel adminModel = currentAdmin(params);
        params.setStoreId(adminModel.getStore_id());
        params.setAdminId(adminModel.getId().toString());
        return Result.success(storeOrderFeign.editEstimatedDeliveryTime(params));
    }

    @ApiOperation("修改实际发货时间")
    @HttpApiMethod(apiKey = "admin.orderV2.editRealDeliveryTime")
    @PostMapping("/editRealDeliveryTime")
    public Result editRealDeliveryTime(@Validated StoreEditRealDeliveryTimeParams params){
        AdminModel adminModel = currentAdmin(params);
        params.setStoreId(adminModel.getStore_id());
        params.setAdminId(adminModel.getId().toString());
        return Result.success(storeOrderFeign.editRealDeliveryTime(params));
    }

    @PostMapping("/editInspection")
    @ApiOperation("修改质检单")
    @HttpApiMethod(apiKey = "admin.orderV2.editInspection")
    public Result editInspection(@Validated StoreInspectionParams params){
        AdminModel adminModel = currentAdmin(params);
        params.setStoreId(adminModel.getStore_id());
        params.setAdminId(adminModel.getId());
        return Result.success(storeOrderFeign.editInspection(params));
    }

    //修改发货单
    @PostMapping("/editInvoice")
    @ApiOperation("修改发货单")
    @HttpApiMethod(apiKey = "admin.orderV2.editInvoice")
    public Result  editInvoice(@Validated StoreInvoiceParams params){
        AdminModel adminModel = currentAdmin(params);
        params.setStoreId(adminModel.getStore_id());
        params.setAdminId(adminModel.getId());
        return Result.success(storeOrderFeign.editInvoice(params));
    }


    @ApiOperation("采购订单导出")
    @PostMapping("/exportOrder")
    @HttpApiMethod(urlMapping = {"admin.orderV2.exportOrder"})
    public Result export(@Validated StoreOrderInfoParams params, HttpServletResponse response){
        AdminModel adminModel = currentAdmin(params);
        params.setStoreId(adminModel.getStore_id());
        params.setAdminId(adminModel.getId());
        Workbook workbook = null;
        OutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            StoreOrderDTO orderInfo = storeOrderFeign.getOrderInfo(params);
            workbook = exportService.export(orderInfo);

            response.setContentType("application/msexcel");
            String title = String.format("订单%s.xls", params.getOrderNo());
            response.addHeader("Content-Disposition", "filename=" + URLDecoder.decode(title, GloabConst.Chartset.UTF8));
            workbook.write(outputStream);
            return null;
        } catch (Exception e) {

        } finally {
            if (workbook != null) {
                try {
                    workbook.close();
                } catch (IOException e) {

                }
            }
            if(outputStream != null){
                try {
                    outputStream.close();
                } catch (IOException e) {

                }
            }
        }
        return Result.error("导出失败");
    }
    @ApiOperation("工厂订单导出")
    @PostMapping("/exportMchOrder")
    @HttpApiMethod(urlMapping = {"admin.orderV2.exportMchOrder"})
    public Result exportMchOrder(@Validated StoreOrderInfoParams params, HttpServletResponse response){
        AdminModel adminModel = currentAdmin(params);
        params.setStoreId(adminModel.getStore_id());
        params.setAdminId(adminModel.getId());
        Workbook workbook = null;
        OutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            StoreOrderDTO orderInfo = storeOrderFeign.getOrderInfo(params);
            workbook = exportService.exportMchOrder(orderInfo);

            response.setContentType("application/msexcel");
            String title = String.format("订单%s.xls", params.getOrderNo());
            response.addHeader("Content-Disposition", "filename=" + URLDecoder.decode(title, GloabConst.Chartset.UTF8));
            workbook.write(outputStream);
            return null;
        } catch (Exception e) {

        } finally {
            if (workbook != null) {
                try {
                    workbook.close();
                } catch (IOException e) {

                }
            }
            if(outputStream != null){
                try {
                    outputStream.close();
                } catch (IOException e) {

                }
            }
        }
        return Result.error("导出失败");
    }

    @ApiOperation("平台改价及备注")
    @PostMapping("/editOrderPriceAndRemark")
    @HttpApiMethod(urlMapping = {"admin.orderV2.editOrderPriceAndRemark"})
    public Result editOrderPriceAndRemark(StoreEditOrderPriceParams params){
        AdminModel adminModel = currentAdmin(params);
        params.setStoreId(adminModel.getStore_id());
        params.setAdminId(adminModel.getId());
        return Result.success(storeOrderFeign.editOrderPriceAndRemark(params));
    }

    @ApiOperation("修改指定发货时间")
    @HttpApiMethod(apiKey = "admin.orderV2.editSpecifyDeliveryTime")
    @PostMapping("/editSpecifyDeliveryTime")
    public Result editSpecifyDeliveryTime(@Validated StoreEditSpecifyDeliveryTimeParams params){
        AdminModel adminModel = currentAdmin(params);
        params.setStoreId(adminModel.getStore_id());
        params.setAdminId(adminModel.getId());
        return Result.success(storeOrderFeign.editSpecifyDeliveryTime(params));
    }


}
